package com.example.controller;

import com.example.helpconnect.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class LocationObtainer extends Activity {
	
	private Location location;
	private LocationManager gpsLocationManager;
	private LocationManager networkLocationManager;
	
	private static final int TEN_SECONDS = 10000;
    private static final int TEN_METERS = 10;
    private static final int TWO_MINUTES = 1000 * 60 * 2;
	
	public LocationObtainer(){
		this.location = null;
		this.gpsLocationManager = null;
		this.gpsLocationManager = null;
	}

	public LocationObtainer(LocationManager gpsLocationManager, LocationManager networkLocationManager){
		this.location = null;
		this.gpsLocationManager = gpsLocationManager;
		this.networkLocationManager = networkLocationManager;
		this.startUpdates();
	}
	
	public void startUpdates()
	{
		final boolean gpsEnabled = this.gpsLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        
        if(!gpsEnabled) {
        	// Create alert dialog here
        	enableLocationSettings();
        }
        
        this.gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, listener);
        this.networkLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 10, listener);
        
	}
	
	public Location getLocation(){
		return this.location;
	}
	
	public Double getLatitude(){
		return this.location.getLatitude();
	}
	
	public Double getLongitude(){
		return this.location.getLongitude();
	}
	
	
	
public Location getCurrentLocation(){
		
		System.out.println("IN LocationObtainer getCurrentLocation!!");
        final boolean gpsEnabled = this.gpsLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        
        if(!gpsEnabled) {
        	// Create alert dialog here
        	enableLocationSettings();
        }
        
        //this.gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, listener);
        //this.networkLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 10, listener);
        
        Location newGPSLoc = this.gpsLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        Location newNETLoc = this.networkLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        
        Location bestLoc;
        if(newGPSLoc == null){
        	bestLoc = newNETLoc;
        	System.out.println("newNETLoc: " + newNETLoc.getLatitude() + ", " + newNETLoc.getLongitude());
            
        }
        else{
        	bestLoc = getBetterLocation(newNETLoc, newGPSLoc);
        	 System.out.println("newGPSLoc: " + newGPSLoc.getLatitude() + ", " + newGPSLoc.getLongitude());
             System.out.println("newNETLoc: " + newNETLoc.getLatitude() + ", " + newNETLoc.getLongitude());
             
        }
        
        System.out.println("bestLoc: " + bestLoc.getLatitude() + ", " + bestLoc.getLongitude());
        this.location = bestLoc;
        return bestLoc;

	}
	
	private void enableLocationSettings(){
    	Intent settingsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    	startActivity(settingsIntent);
    }
	
	private final LocationListener listener = new LocationListener() {
    	
    	@Override
    	public void onLocationChanged(Location location) {
    		System.out.println("updated location: " + location.getLatitude() + ", " + location.getLongitude());
    	}
    	
    	@Override
        public void onStatusChanged(String s, int i, 
                    Bundle bundle) {
        }
        @Override
        public void onProviderEnabled(String s) {
        }
        @Override
        public void onProviderDisabled(String s) {
        }
    };
	

 // Stop receiving location updates whenever the Activity becomes invisible.
    protected void stopUpdates() {
        this.gpsLocationManager.removeUpdates(listener);
        this.networkLocationManager.removeUpdates(listener);
    }
    
   
    
    /** Determines whether one Location reading is better than the current Location fix.
     * Code taken from
     * http://developer.android.com/guide/topics/location/obtaining-user-location.html
     *
     * @param newLocation  The new Location that you want to evaluate
     * @param currentBestLocation  The current Location fix, to which you want to compare the new
     *        one
     * @return The better Location object based on recency and accuracy.
     */
   protected Location getBetterLocation(Location newLocation, Location currentBestLocation) {
       if (currentBestLocation == null) {
           // A new location is always better than no location
           return newLocation;
       }

       // Check whether the new location fix is newer or older
       long timeDelta = newLocation.getTime() - currentBestLocation.getTime();
       boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
       boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
       boolean isNewer = timeDelta > 0;

       // If it's been more than two minutes since the current location, use the new location
       // because the user has likely moved.
       if (isSignificantlyNewer) {
           return newLocation;
       // If the new location is more than two minutes older, it must be worse
       } else if (isSignificantlyOlder) {
           return currentBestLocation;
       }

       // Check whether the new location fix is more or less accurate
       int accuracyDelta = (int) (newLocation.getAccuracy() - currentBestLocation.getAccuracy());
       boolean isLessAccurate = accuracyDelta > 0;
       boolean isMoreAccurate = accuracyDelta < 0;
       boolean isSignificantlyLessAccurate = accuracyDelta > 200;

       // Check if the old and new location are from the same provider
       boolean isFromSameProvider = isSameProvider(newLocation.getProvider(),
               currentBestLocation.getProvider());

       // Determine location quality using a combination of timeliness and accuracy
       if (isMoreAccurate) {
           return newLocation;
       } else if (isNewer && !isLessAccurate) {
           return newLocation;
       } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
           return newLocation;
       }
       return currentBestLocation;
   }
   
   
   /** Checks whether two providers are the same */
   private boolean isSameProvider(String provider1, String provider2) {
       if (provider1 == null) {
         return provider2 == null;
       }
       return provider1.equals(provider2);
   }
	
	
	
}
